
angular.module('U9ImHd.controllers')

.controller('RegisterCtrl', ['$q', '$scope', '$ionicHistory', 'RegisterService', 'Device', 'imanDialog', 'InitService', 
    function ($q, $scope, $ionicHistory, RegisterService, Device, imanDialog, InitService) {
        $scope.data = {
            uuid: '',
            ip: '',
            mac: '',
            workLocationName: '',
            hasLastRegister: false,
            lastRegisterTime: '',
            lastRegisterStation: ''
        };

        var stationList = [],
            workLocationID = '',
            workLocationCode = '';

        $scope.$on('$ionicView.afterEnter', function () {
            InitService.then(init);
        });

        $scope.selectStation = function () {
            imanDialog.showList({
                title: '请选择终端工位',
                list: stationList,
                valueField: 'Code',
                displayField: 'Name',
                value: workLocationCode,
                supportSearch: true,
                searchBy: 'Code'
            }).then(function (select) {
                $scope.data.workLocationName = select.Name;
                workLocationID = select._id;
                workLocationCode = select.Code;
            });
        };
        $scope.registerTerminal = function () {
            if (!$scope.data.uuid) {
                imanDialog.toastCenter('设备唯一标识IMEI不能为空');
                return;
            }
            if (!workLocationID) {
                imanDialog.toastCenter('请选择终端工位');
                return;
            }
            RegisterService.registerTerminal($scope.data.uuid, workLocationID).then(function () {
                Device.updateLastRegister().finally(function () {
                    $ionicHistory.goBack();
                });
            });
        };

        function init () {
            imanDialog.showLoading();
            var tasks = [];
            $scope.data.uuid = Device.getUUID();
            if (window.cordova) {
                var getIpAddress = Device.getIpAddress().then(function (ip) {
                    $scope.data.ip = ip;
                });
                tasks.push(getIpAddress);
                var getMacAddress = Device.getMacAddress().then(function (mac) {
                    $scope.data.mac = mac;
                });
                tasks.push(getMacAddress);
            }
            var getStation = RegisterService.getStation().then(function (stations) {
                stationList = stations;
            });
            tasks.push(getStation);
            $q.all(tasks).finally(function () {
                getLastRegister();
                imanDialog.hideLoading();
            });
        }

        function getLastRegister () {
            var workLocation = Device.get('WorkLocation');
            if (workLocation) {
                $scope.data.lastRegisterTime = workLocation.ModifyOn;
                $scope.data.workLocationName = workLocation.Name;
                $scope.data.lastRegisterStation = workLocation.Name;
                workLocationID = workLocation._id;
                workLocationCode = workLocation.Code;
                $scope.data.hasLastRegister = true;
            } else {
                $scope.data.lastRegisterTime = '';
                $scope.data.lastRegisterStation = '';
                $scope.data.hasLastRegister = false;
            }
        }
    }
]);